package com.boolib.tree;


import com.boolib.tree.entity.BaseTreeGrid;
import com.boolib.tree.exception.IsTopCategoryException;
import com.boolib.tree.exception.NotFoundItemException;
import com.boolib.tree.face.ITreeModelHandler;


import java.util.*;


public class TreeModelHandlerImpl<T extends BaseTreeGrid> implements ITreeModelHandler {

    List<T> beforeList = null;

    List<T> afterNode = null;

    Map<Integer,T> idMap = null;

    Map <Integer,List<T>> sameParentMap = null;






    public List<T> getAfterNode() {
        return afterNode;
    }


    public TreeModelHandlerImpl(List<T> beforeList1){

        this.beforeList = beforeList1;

        afterNode= dfs();

        idMap = new HashMap<>();


        sameParentMap = new HashMap<>();



        for(T t : beforeList){

            idMap.put(t.getId(),t);

            Integer parentId = t.getParentId();


            List<T> item  = sameParentMap.get(parentId);

            if(item==null){
                item = new ArrayList<>();
            }

            item.add(t);

            sameParentMap.put(parentId,item);

        }

    }



    private  int deep = 0 ;

    private List<T> dfs(){

        List<T> firstCategory = new ArrayList<>();

        for(T t : beforeList){

            if(t.getParentId().equals(0))
                firstCategory.add(t);
        }

        for (T t:firstCategory) {

            t=dfs(t,beforeList);
        }


        return firstCategory;

    }


    private T dfs(T item,List<T> all){

        List<T> children = new ArrayList<>();


        for(T i :all){
            if(i.getParentId().equals(item.getId())){
                dfs(i,all);
                children.add(i);
                item.setChildren(children);
            }
        }

        return  item;
    }




    @Override
    public T findParent(Integer id) throws NotFoundItemException, IsTopCategoryException {

        T item = idMap.get(id);


        if(item==null) throw new NotFoundItemException("没有找到当前");

        if(item.getParentId().equals(0))
            throw new IsTopCategoryException("已经是最顶层了");

        item =  idMap.get(item.getParentId());

        if(item==null) throw new NotFoundItemException("没有找到当前");

        return item ;
    }

    @Override
    public Integer findParentId(Integer id) throws NotFoundItemException {
        T item = idMap.get(id);


        if(item==null) throw new NotFoundItemException("没有找到当前");

        return item.getParentId();

    }

    @Override
    public List findParents(Integer id) throws NotFoundItemException {

        return findPs(id,new ArrayList<>());
    }

    private List findPs(Integer id , List<T> list) throws NotFoundItemException {


        //获取当前
        T item = idMap.get(id);

        if(item==null) throw new NotFoundItemException("没有找到当前");


        //获取父亲id

        while(!item.getParentId().equals(0)){

            System.out.println(item.getParentId());

            //获取父亲节点
            item = idMap.get(item.getParentId());


            if(item==null) throw new NotFoundItemException("没有找到当前");

            list.add(item);

        }


        Collections.sort(list, new Comparator<T>() {
            @Override
            public int compare(T o1, T o2) {

                return o1.getId().compareTo(o2.getId());
            }
        });
        return list;

    }



    @Override
    public List<Integer> findParentsId(Integer id) throws NotFoundItemException {

        List<Integer> lists = new ArrayList<>();

        //获取当前
        T item = idMap.get(id);

        if(item==null) throw new NotFoundItemException("没有找到当前");


        //获取父亲id

        while(!item.getParentId().equals(0)){

            System.out.println(item.getParentId());

            //获取父亲节点
            item = idMap.get(item.getParentId());


            if(item==null) throw new NotFoundItemException("没有找到当前");

            lists.add(item.getId());


        }
        Collections.sort(lists, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        return  lists;
    }

    @Override
    public List<T> findBrothers(Integer id) throws NotFoundItemException {
        T item = idMap.get(id);

        if(item==null) throw new NotFoundItemException("没有找到当前");


        return  sameParentMap.get(item.getParentId());

    }

    @Override
    public List<Integer> findBrothersId(Integer id) throws NotFoundItemException {


        T item = idMap.get(id);

        if(item==null) throw new NotFoundItemException("没有找到当前");

        List<Integer> list = new ArrayList<>();

        if(sameParentMap.get(item.getParentId())==null) return null;

        for (T var1  : sameParentMap.get(item.getParentId()))
        {
                list.add(var1.getId());
        }

        return list;
    }

    @Override
    public List<T> findChildren(Integer id) throws NotFoundItemException {


        if(sameParentMap.get(id)==null) return null;



        return  sameParentMap.get(id);
    }

    @Override
    public List<Integer> findChildrenId(Integer id) throws NotFoundItemException {

        if(sameParentMap.get(id)==null) return null;

        List<Integer> list = new ArrayList<>();



        for (T var1  : sameParentMap.get(id))
        {
            list.add(var1.getId());
        }

        return list;

    }


}
